<?php
/*
 * @灯商云端
 * @功能说明：用户中心
 * @更新说明：暂无更新
 * @文件名 UserOldController.class.php
 * @编码 UTF-8
 * @创建时间 2015-7-21 下午3:38:52
 * @创建人 XING
 */

namespace home\Controller;

use dg\Model\ResEntityModel;

use Think\Controller;
use service\excel\PhpExcel;

class UserController extends ApibaseController
{
    function __construct() {
        parent::__construct();
        $this->_ResModel = D('ResEntity');
    }

    /*
     * 登陆模块
     * @date: 2015-3-21下午2:10:37
     * @editor: XING
     */
    public function login() {
        global $isLogin;
        if ($isLogin){
            if(session('role_id') == 5){
                //2017-9-9新增
                redirect('/Community/policyType?res=0-1');
            }
            redirect('/user/admin?res=0-1');
        }else{
            $this->display('user/login');
        }
    }

    /*
     * 系统用户注册
     * @date: 2015-3-21下午2:20:22
     * @editor: XING
     */

    public function regist() {
    	throw new \Exception('非法请求');exit;
        //注册页面渲染
        $this->display('user/regist');
    }

    /*
     * 系统登陆
     * @date: 2015-3-21下午2:48:06
     * @editor: XING
     */
    public function checklogin() {
//        echo I('post.username');exit;

        $userMsg = M('ResEntity')->where(array('username' => I('post.username')))->find();

        if ($userMsg['password'] == md5(I('post.passwd') . $userMsg['salt'])) {

            $pushSession = array('name' => I('post.username'));
            if (I('post.signlogin') == 1) {
                $setSecret = md5(I('post.username') . 'ajwUid' . time());
                M('ResEntity')->where(array('username' => I('post.username')))->setField(
                    array('remember_token' => $setSecret)
                );

                cookie('_ajwunion', $setSecret, 'expire=604800');
                $pushSession['expire'] = 604800;
            }
            $role_id = M('AuthGroupAccess')->where(['uid'=>$userMsg['rid']])->getField('role_id');
            session($pushSession);
            session('role_id',$role_id);
            session('loginUid', $userMsg['rid']);

            cookie('ajwUid', base64_encode(I('post.username')) . randstr(), 'expire=604800');

            session(I('post.username'), true);
           // echo 2332;exit;
            //putlog('登陆系统', 'UserController', 'checklogin');
            jsonRespons('1', '登陆成功！');
        } else {

            jsonRespons('0', '帐号或密码有误！');
        }
    }

    /*
	 * 美潮风_临时租用
	 * @date: 2016-4-8 下午5:11:18
	 * @editor: XING
	 */
    public function printMsg() {
        if (isset($_GET) && $_GET) {
            file_put_contents('123.txt', json_encode($_GET));
        } else {
            file_put_contents('123.txt', 'nodata');
        }
    }

    /*
     * 系统注销操作
     * @date: 2015-3-23下午12:02:30
     * @editor: XING
     */
    public function loginout() {
        $userMsg = getLoginMsg();
        session('role_id',null);
        session('loginUid',null);
        session(cookie('PHPSESSID'), null);
        session(getLoginMsg()->username, null);
        session('role_id',null);
        cookie('ajwUid', null);
        cookie('_ajwunion', null);
        cookie('PHPSESSID', null);
        redirect('/user/login');
    }

    /*
     * 个人中心简介
     * @date: 2015-3-23下午2:30:20
     * @editor: XING
     */
    public function usercenter() {

        //个人页面渲染
        $this->display('user/usercenter');
    }

    /*
     * 个人中心资料编辑
     * @date: 2015-3-23下午5:06:13
     * @editor: XING
     */
    public function userUpdate() {
        if (I('post.update')) {
            $data = array('email' => I('post.email'));
            if (I('post.password')) {
                $salt = randstr(5);
                $data['password'] = md5(I('post.password') . $salt);
                $data['salt'] = $salt;
            }
            $data['email'] = I('post.email');
            session('kfLine' . I('post.rid'), I('post.lineStatus') == '1' ? '1' : null);
            $keArr = session('kfLineArr');
            if (I('post.lineStatus') == '1') {
                if (isset($keArr[I('post.rid')]) && $keArr[I('post.rid')] = I('post.rid')) {
                    // Nothing to do
                } else {
                    $keArr[I('post.rid')] = I('post.rid');
                }
                session('kfLineArr', $keArr);
            } else {
                if (isset($keArr[I('post.rid')])) {
                    unset($keArr[I('post.rid')]);
                    session('kfLineArr', $keArr);
                }
            }
            M('ResEntity')->where(array('rid' => session('loginUid')))->setField($data);

            $this->showtips('信息编辑成功');
        }
    }

    /*
     * 后台管理员管理
     * @date: 2016-9-21 上午15:41:13
     * @editor: liuqi
     */
    public function admin(){
    	if(!is_login()){
    		redirect('/user/login');exit;
    	}
        $admin = M('ResEntity e')
                ->field('e.rid,e.username,r.name,e.email,e.add_at')
                ->join('trb_auth_group_access a on a.uid=e.rid')
                ->join('trb_role r on r.id=a.role_id')
                ->where(['e.is_active'=>1])
                ->select();
        $this->assign('getData',$admin);
        $this->display('user/admin');
    }

    /*
     * 后台管理员管理
     * @date: 2016-9-21 上午15:51:26
     * @editor: liuqi
     */
    public function addAdmin(){
    	if(!is_login()){
    		redirect('/user/login');exit;
    	}
        /*用户注册逻辑*/
        if(I('post.submit')){
            if(I('post.rid')){
                $data = array('email'=>I('post.email'));
                if(I('post.password')){
                    $salt = randstr(5);
                    $data['password'] = md5(I('post.password').$salt);
                    $data['salt'] = $salt;
                }
                $data['username'] = I('post.username');
                if(I('post.role_id')){
                    M('AuthGroupAccess')->where(['uid'=>I('post.rid')])->setField(['role_id'=>I('post.role_id')]);
                    $data['role_id'] = I('post.role_id');
                }
                M('ResEntity')->where(['rid'=>I('post.rid')])->setField($data);
                putlog('修改管理员'.$data['username'].'的信息','UserController','addAdmin');
                $this->showtips('信息编辑成功');
            }
            $salts = randstr(5);
            $data = [
                    'username'          => I('post.username'),
                    'password'          => md5(I('post.password').$salts),
                    'salt'              => $salts,
                    'email'             => I('post.email'),
                    'role_id'           => I('post.role_id'),
                    'remember_token'    => '',
                    'add_at'            => time(),
                    'update_at'         => time()
                ];
            $res = M('ResEntity')->add($data);
            if(I('post.role_id')){
                M('AuthGroupAccess')->add(['uid'=>$res,'role_id'=>I('post.role_id')]);
            }
            putlog('添加管理员'.I('post.username'),'UserController','addAdmin');
            if($res){
                $this->showtips('添加管理员成功');
            }else{
                $this->showtips('操作失败');
            }
        }
        $admin_info = M('resEntity e')
                    ->join('trb_auth_group_access a on a.uid=e.rid')
                    ->join('trb_role r on r.id=a.role_id')
                    ->where(['e.rid'=>I('get.rid')])
                    ->find();
        $this->assign('admin_info',$admin_info);
        $role = M('role')->where(['status'=>1])->select();
        $this->assign('role',$role);
        //注册页面渲染
        $this->display('user/addadmin');
    }
}